﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using Microsoft.Office.Interop.Excel;
using TpBaseObjects.Properties;
using TpExtensions;
using TpLogger;
using System.Diagnostics;


namespace TpBaseObjects.Base {
    public class JobRow {

        #region Members

        private Range myRow;

        #endregion

        #region Contructor

        public JobRow(Range row) {
            myRow = row;
        }

        #endregion

        #region Properties

        public double AWDifferenzErfassung {
            get {
                double s1 = 0;
                Range r1 = myRow.Cells[1, "BJ"];

                if (r1.Value != null) {
                    if (Double.TryParse(r1.Value.ToString(), out s1)) {
                        return s1;
                    }
                }
                return s1;
            }
            set {
                double s1 = value;
                Range r1 = myRow.Cells[1, "BJ"];
                r1.Value = s1;
            }
        }

        public double AWDifferenzPauschal {
            get {
                double s1 = 0;
                Range r1 = myRow.Cells[1, "BL"];
                if (r1.Value != null) {
                    if (Double.TryParse(r1.Value.ToString(), out s1)) {
                        return s1;
                    }
                }
                return s1;
            }
            set {
                double s1 = value;
                Range r1 = myRow.Cells[1, "BL"];
                r1.Value = s1;
            }
        }

        public double AWDifferenzPauschalProzent {
            get {
                double s1 = 0;
                Range r1 = myRow.Cells[1, "BM"];
                if (r1.Value != null) {
                    if (Double.TryParse(r1.Value.ToString(), out s1)) {
                        return s1;
                    }
                }
                return s1;
            }
            set {
                double s1 = value;
                Range r1 = myRow.Cells[1, "BM"];
                r1.Value = s1;
            }
        }

        public double AWStunden {
            get {
                double s1 = 0;
                Range r1 = myRow.Cells[1, "BK"];
                if (r1.Value != null) {
                    if (Double.TryParse(r1.Value.ToString(), out s1)) {
                        return s1;
                    }
                }
                return s1;
            }
            set {
                double s1 = value;
                Range r1 = myRow.Cells[1, "BK"];
                r1.Value = s1;
            }
        }

        public string AWR1C1DifferenzErfassung {
            get {
                string s1 = "";
                Range r1 = myRow.Cells[1, "BJ"];

                if (r1.Value != null) {
                    s1 = r1.FormulaR1C1;
                }
                return s1;
            }
            set {
                string s1 = Convert.ToString(value);
                Range r1 = myRow.Cells[1, "BJ"];
                r1.FormulaR1C1 = s1;
            }
        }

        public string AWR1C1DifferenzPauschal {
            get {
                string s1 = "";
                Range r1 = myRow.Cells[1, "BL"];
                if (r1.Value != null) {
                    s1 = r1.FormulaR1C1;
                }
                return s1;
            }
            set {
                string s1 = Convert.ToString(value);
                Range r1 = myRow.Cells[1, "BL"];
                r1.FormulaR1C1 = s1;
            }
        }

        public string AWR1C1DifferenzPauschalProzent {
            get {
                string s1 = "";
                Range r1 = myRow.Cells[1, "BM"];
                if (r1.Value != null) {
                    s1 = r1.FormulaR1C1;
                }
                return s1;
            }
            set {
                string s1 = Convert.ToString(value);
                Range r1 = myRow.Cells[1, "BM"];
                r1.FormulaR1C1 = s1;
            }
        }

        public string AWR1C1Stunden {
            get {
                string s1 = "";
                Range r1 = myRow.Cells[1, "BK"];
                if (r1.Value != null) {
                    s1 = r1.FormulaR1C1;
                }
                return s1;
            }
            set {
                string s1 = Convert.ToString(value);
                Range r1 = myRow.Cells[1, "BK"];
                r1.FormulaR1C1 = s1;
            }
        }

        public string AWR1C1SumPerDay {
            get {
                string s1 = "";
                Range r1 = myRow.Cells[1, "AW"];
                if (r1.Value != null) {
                    s1 = r1.FormulaR1C1;
                }
                return s1;
            }
            set {
                string s1 = Convert.ToString(value);
                Range r1 = myRow.Cells[1, "AW"];
                r1.FormulaR1C1 = s1;
            }
        }

        public string Durchführender1 {
            get {
                Range r1 = myRow.Cells[1, Settings.Default.SpalteDurchführender1];
                string t1 = Extensions.Nz(Convert.ToString(r1.Value), "");
                if (!Extensions.TpUser.Contains(t1)) {
                    t1 = "";
                }
                return t1;
            }
            set {
                Range r1 = myRow.Cells[1, Settings.Default.SpalteDurchführender1];
                r1.Value = value;
            }
        }

        public string Durchführender2 {
            get {
                Range r1 = myRow.Cells[1, Settings.Default.SpalteDurchführender2];
                string t1 = Extensions.Nz(Convert.ToString(r1.Value), "");
                if (!Extensions.TpUser.Contains(t1)) {
                    t1 = "";
                }
                return t1;
            }
            set {
                Range r1 = myRow.Cells[1, Settings.Default.SpalteDurchführender2];
                r1.Value = value;
            }
        }

        public DateTime JobDatum {
            get {
                DateTime ret = new DateTime(1900, 1, 1);
                Range r1 = myRow.Cells[1, Settings.Default.SpalteJobDatum];
                if (r1.Value != null) {
                    if (DateTime.TryParse(r1.Value.ToString(), out ret)) {
                        return ret;
                    }
                }
                return ret;
            }
            set {
                Range r1 = myRow.Cells[1, Settings.Default.SpalteJobDatum];
                if (value != new DateTime(1900, 1, 1)) {
                    r1.Value = value.ToString("yyyy/MM/dd");
                } else {
                    r1.Value = "";
                }
            }
        }

        public double JobTime {
            get {
                double s1 = 0;
                Range r1 = myRow.Cells[1, "P"];
                if (r1.Value != null) {
                    if (Double.TryParse(r1.Value.ToString(), out s1)) {
                        return s1;
                    }
                }
                return s1;
            }
            set {
                double s1 = value;
                Range r1 = myRow.Cells[1, "P"];
                r1.Value = s1;
            }
        }

        public int JobType {
            get {
                int s1 = 0;
                Range r1 = myRow.Cells[1, "I"];
                if (r1.Value != null) {
                    Int32.TryParse(Convert.ToString(r1.Value), out s1);
                }
                return s1;
            }
            set {
                int s1 = value;
                Range r1 = myRow.Cells[1, "I"];
                r1.Value = s1;
            }
        }

        public double JobEndTime {
            get {
                double s1 = 0;
                Range r1 = myRow.Cells[1, "Q"];
                if (r1.Value != null) {
                    if (Double.TryParse(r1.Value.ToString(), out s1)) {
                        return s1;
                    }
                }
                return s1;
            }
            set {
                double s1 = value;
                Range r1 = myRow.Cells[1, "Q"];
                r1.Value = s1;
            }
        }

        public string JobOrt {
            get {
                Range r1 = myRow.Cells[1, "M"];
                string t1 = "";
                if (r1.Value != null) {
                    t1 = Convert.ToString(r1.Value);
                }
                return t1;
            }
            set {
                Range r1 = myRow.Cells[1, "M"];
                r1.Value = value;
            }
        }

        public int JobWeek {
            get {
                DateTimeFormatInfo dfi = DateTimeFormatInfo.CurrentInfo;
                Calendar cal = dfi.Calendar;
                return cal.GetWeekOfYear(this.JobDatum, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday);
            }
        }

        public bool IsEmpty {
            get {
                if (this.JobOrt == "") {
                    return true;
                }

                if (this.LeistungZeitSumme == 0) {
                    return true;
                }

                return false;
            }
        }

        public bool IsProcessed {
            get {
                bool ret = false;
                Range r1 = myRow.Cells[1, "CA"];
                if (r1.Value == null) {
                    if (bool.TryParse(r1.Value.ToString(), out ret)) {
                        return ret;
                    }
                }
                return ret;
            }
            set {
                Range r1 = myRow.Cells[1, "CA"];
                string v = value.ToString();
                if (v == "True" || v == "False") {
                    r1.Value = value;
                }
            }
        }

        public string Leistung {
            get {
                Range r1 = myRow.Cells[1, "AH"];
                string t1 = "";
                if (r1.Value != null) {
                    t1 = Convert.ToString(r1.Value);
                }
                return t1;
            }
            set {
                Range r1 = myRow.Cells[1, "AH"];
                r1.Value = value;
            }
        }

        public bool LeistungExported {
            get {
                bool ret = false;
                Range r1 = myRow.Cells[1, Settings.Default.SpalteExported];
                if (r1.Value != null) {
                    string t1 = Extensions.Nz(Convert.ToString(r1.Value), "");
                    if (t1.ToUpper() == "LE") {
                        return true;
                    }
                }
                return ret;
            }
        }

        public double LeistungProzent {
            get {
                double s1 = 0;
                Range r1 = myRow.Cells[1, Settings.Default.SpalteLeistungProzent];
                if (r1.Value != null) {
                    if (Double.TryParse(r1.Value.ToString(), out s1)) {
                        return s1;
                    }
                }
                return s1;
            }
            set {
                Range r1 = myRow.Cells[1, Settings.Default.SpalteLeistungProzent];
                r1.Value = value;
            }
        }

        public double LeistungZeit {
            get {
                double s1 = 0;
                Range r1 = myRow.Cells[1, Settings.Default.SpalteLeistungZeit];
                if (r1.Value != null) {
                    if (Double.TryParse(r1.Value.ToString(), out s1)) {
                        return s1;
                    }
                }
                return s1;
            }
            set {
                Range r1 = myRow.Cells[1, Settings.Default.SpalteLeistungZeit];
                r1.Value = value;
            }
        }

        public double PauschalZeit {
            get {
                double s1 = 0;
                Range r1 = myRow.Cells[1, "U"];
                if (r1.Value != null) {
                    if (Double.TryParse(r1.Value.ToString(), out s1)) {
                        return s1;
                    }
                }
                return s1;
            }
            set {
                Range r1 = myRow.Cells[1, "U"];
                r1.Value = value;
            }
        }

        public double LeistungZeitSumme {
            get {
                double s1 = 0;
                Range r1 = myRow.Cells[1, "AV"];
                if (r1.Value != null) {
                    if (Double.TryParse(r1.Value.ToString(), out s1)) {
                        return s1;
                    }
                }
                return s1;
            }
            set {
                Range r1 = myRow.Cells[1, "AV"];
                r1.Value = value;
            }
        }

        public double Menge {
            get {
                double s1 = 0;
                Range r1 = myRow.Cells[1, Settings.Default.SpalteMenge];
                if (r1.Value != null) {
                    if (Double.TryParse(r1.Value.ToString(), out s1)) {
                        return s1;
                    }
                }
                return s1;
            }
            set {
                Range r1 = myRow.Cells[1, Settings.Default.SpalteMenge];
                r1.Value = value;
            }
        }

        public string RowID {
            get {
                Range r1 = myRow.Cells[1, "BA"];
                string ret = "";
                if (r1.Value != null) {
                    string t1 = Convert.ToString(r1.Value);
                    return Extensions.Nz(t1, "");
                }
                return ret;
            }
            set {
                Range r1 = myRow.Cells[1, "BA"];
                r1.Value = value;
            }
        }

        public int RowNumber {
            get {
                return myRow.Row;
            }
        }

        public string Veranlasser {
            get {
                Range r1 = myRow.Cells[1, "AW"];
                string ret = "";
                if (r1.Value != null) {
                    ret = Convert.ToString(r1.Value);
                }
                return ret;
            }
            set {
                Range r1 = myRow.Cells[1, "AW"];
                r1.Value = value;
            }

        }

        public DateTime VeranlasserDatum {
            get {
                DateTime ret = new DateTime(1900, 1, 1);
                Range r1 = myRow.Cells[1, "AX"];
                if (r1.Value != null) {
                    if (DateTime.TryParse(r1.Value.ToString(), out ret)) {
                        return ret;
                    }
                }
                return ret;
            }
            set {
                Range r1 = myRow.Cells[1, "AX"];
                if (value != new DateTime(1900, 1, 1)) {
                    r1.Value = value.ToString("yyyy/MM/dd");
                } else {
                    r1.Value = "";
                }
            }
        }

        #endregion

        #region Methods

        public void DeleteBorderUnderline() {
            Range r1 = myRow;
            try {
                r1.Select();
            } catch(Exception ex) {
                Debug.Print(ex.Message);
                //do nothing
            } finally {
                //do nothing
                
            }
            r1.Borders[XlBordersIndex.xlEdgeBottom].LineStyle = XlLineStyle.xlLineStyleNone;

        }

        public bool DeleteMarkup(out string errMsg) {
            bool ret = false;
            errMsg = "";
            try {
                Range r1 = myRow.Cells[1, "P"];
                r1.Interior.Pattern = XlPattern.xlPatternNone;
                r1 = myRow.Cells[1, "Q"];
                r1.Interior.Pattern = XlPattern.xlPatternNone;
                r1 = myRow.Cells[1, "AW"];
                r1.Value = "";
                ret = true;
            } catch (Exception ex) {
                errMsg = ex.Message;
                ret = false;
            }
            return ret;
        }

        public void DrawBorderUnderline() {
            Range r1 = myRow;
            try {
                r1.Select();
            } catch (Exception ex) {
                Debug.Print(ex.Message);
                //do nothing
            } finally {
                //do nothing

            }
            r1.Borders[XlBordersIndex.xlEdgeBottom].LineStyle = XlLineStyle.xlContinuous;
            r1.Borders[XlBordersIndex.xlEdgeBottom].Weight = XlBorderWeight.xlHairline;
        }

        public void MarkJobEndTimeAsFaild() {
            Range r1 = myRow.Cells[1, "Q"];
            
            r1.Interior.Color = Global.RGB(0, 255, 255);
        }

        public void MarkJobTimeAsFaild() {
            Range r1 = myRow.Cells[1, "P"];
            r1.Interior.Color = Global.RGB(0, 255, 255);
        }

        #endregion
    }
}
